#include "PlatformProperty.h"

#define  KEYCODE_CAR_TOP     10000
#define  KEYCODE_CAR_BOTTOM  10001
#define  KEYCODE_CAR_LEFT    10002
#define  KEYCODE_CAR_RIGHT   10003
#define  KEYCODE_CAR_CONFIRM 10004
#define  KEYCODE_CAR_VR      10005
#define  KEYCODE_CAR_CUSTOM  10006
#define  KEYCODE_CAR_MENU    10007
#define  KEYCODE_CAR_MUTE    10008

#define  KEYCODE_CAR_DRIVE_MODE   10009
#define  KEYCODE_CAR_PANEL_CUSTOM 10010
#define  KEYCODE_CAR_POWER        10011
#define  KEYCODE_CAR_RACE_MODE    10012
 
//[SW_FC_029_003] 方控按键
SYSTEM_READ_PROPERTY(HW_CUSTOM_INPUT, std::vector<int32_t>) {
    auto setWheelKeyValue = [=](const int32_t keycode,const sdb::WheelKeyEvent event) {
        sdb::WheelKeyModeStatus mode = SIGNAL.getWheelKeyModeSync();
        VHALOG.d("HW_CUSTOM_INPUT halWithValueBySignals : %d", mode);
        if (mode == sdb::WheelKeyModeStatus::Normal
            || mode == sdb::WheelKeyModeStatus(-1)
            || mode == sdb::WheelKeyModeStatus::Mode_HUD_10s_TimeOut
            || mode == sdb::WheelKeyModeStatus::Mode_RearMirror_10s_TimeOut
            || mode == sdb::WheelKeyModeStatus::Mode_Elc_Steer_10s_Timeout) {
            std::vector<int32_t> toHalKeyValue = getValue(toInt(VehicleArea::GLOBAL));
            toHalKeyValue[0] = keycode;
            toHalKeyValue[1] = toInt(VehicleDisplay::MAIN);
            if(event == sdb::WheelKeyEvent::Press){
                VHALOG.d("HW_CUSTOM_INPUT keycode: %d Press",keycode);
                toHalKeyValue[2] = 0;//按下
                setValue(toInt(VehicleArea::GLOBAL), toHalKeyValue);
            }else if(event == sdb::WheelKeyEvent::Release){
                VHALOG.d("HW_CUSTOM_INPUT keycode: %d Release",keycode);
                toHalKeyValue[2] = -1;//释放
                setValue(toInt(VehicleArea::GLOBAL), toHalKeyValue);
            }
        }
    };

    auto setScreenKeyValue = [=](const int32_t keycode,const sdb::PsdNotPsd event) {
        std::vector<int32_t> toHalKeyValue = getValue(toInt(VehicleArea::GLOBAL));
        toHalKeyValue[0] = keycode;
        toHalKeyValue[1] = toInt(VehicleDisplay::MAIN);
        if(event == sdb::PsdNotPsd::Psd){
            VHALOG.d("HW_CUSTOM_INPUT keycode: %d Press",keycode);
            toHalKeyValue[2] = 0;//按下
            setValue(toInt(VehicleArea::GLOBAL), toHalKeyValue);
        }else if(event == sdb::PsdNotPsd::NotPsd){
            VHALOG.d("HW_CUSTOM_INPUT keycode: %d Release",keycode);
            toHalKeyValue[2] = -1;//释放
            setValue(toInt(VehicleArea::GLOBAL), toHalKeyValue);
        }
    };

    builder.area(VehicleArea::GLOBAL)
            .initValue([]() {
                std::vector<int32_t> defaultVec(3, 0);
                return defaultVec;
            })
            .immutableStatus(PropertyStatus::Active)
            .withValueBySignals([](const std::vector<int32_t> last) {return last;})
            .subscribeSignals(
                [=]() { //Top
                    sdb::WheelKeyEvent event = SIGNAL.getHardKeyTop();
                    VHALOG.d("HW_CUSTOM_INPUT event [top] : %d",event);
                    setWheelKeyValue(KEYCODE_CAR_TOP,event);
                },APCPSignalRX::HardKeyTop)
            .subscribeSignals(
                [=]() { //Bottom
                    sdb::WheelKeyEvent event = SIGNAL.getHardKeyBottom();
                    VHALOG.d("HW_CUSTOM_INPUT event [bottom] : %d",event);
                    setWheelKeyValue(KEYCODE_CAR_BOTTOM,event);
                },APCPSignalRX::HardKeyBottom)
            .subscribeSignals(
                [=]() { //Left
                    sdb::WheelKeyEvent event = SIGNAL.getHardKeyLeft();
                    VHALOG.d("HW_CUSTOM_INPUT event [left] : %d",event);
                    setWheelKeyValue(KEYCODE_CAR_LEFT,event);
                },APCPSignalRX::HardKeyLeft)
            .subscribeSignals(
                [=]() { //Right
                    sdb::WheelKeyEvent event = SIGNAL.getHardKeyRight();
                    VHALOG.d("HW_CUSTOM_INPUT event [right] : %d",event);
                    setWheelKeyValue(KEYCODE_CAR_RIGHT,event);
                },APCPSignalRX::HardKeyRight)
            .subscribeSignals(
                [=]() { //Confirm
                    sdb::WheelKeyEvent event = SIGNAL.getHardKeyConfirm();
                    VHALOG.d("HW_CUSTOM_INPUT event [confirm] : %d",event);
                    setWheelKeyValue(KEYCODE_CAR_CONFIRM,event);
                },APCPSignalRX::HardKeyConfirm)
            .subscribeSignals(
                [=]() { //VR
                    sdb::WheelKeyEvent event = SIGNAL.getHardKeyVR();
                    VHALOG.d("HW_CUSTOM_INPUT event [vr] : %d",event);
                    setWheelKeyValue(KEYCODE_CAR_VR,event);
                },APCPSignalRX::HardKeyVR)
            .subscribeSignals(
                [=]() { //Menu
                    sdb::WheelKeyEvent event = SIGNAL.getHardKeyMenu();
                    VHALOG.d("HW_CUSTOM_INPUT event [menu] : %d",event);
                    setWheelKeyValue(KEYCODE_CAR_MENU,event);
                },APCPSignalRX::HardKeyMenu)
            .subscribeSignals(
                [=]() { //Custom
                    sdb::WheelKeyEvent event = SIGNAL.getHardKeyCustom();
                    VHALOG.d("HW_CUSTOM_INPUT event [custom] : %d",event);
                    setWheelKeyValue(KEYCODE_CAR_CUSTOM,event);
                },APCPSignalRX::HardKeyCustom)
            .subscribeSignals(
                [=]() { //Driver Mode
                    sdb::PsdNotPsd event = SIGNAL.getDriModBut();
                    VHALOG.d("HW_CUSTOM_INPUT event [drivemode] : %d",event);
                    setScreenKeyValue(KEYCODE_CAR_DRIVE_MODE,event);
                },SignalRX::DriModBut)
            .subscribeSignals(
                [=]() { //panel custom
                    sdb::PsdNotPsd event = SIGNAL.getCusKeyBut();
                    VHALOG.d("HW_CUSTOM_INPUT event [panelcustom] : %d",event);
                    setScreenKeyValue(KEYCODE_CAR_PANEL_CUSTOM,event);
                },SignalRX::CusKeyBut)
            .subscribeSignals(
                [=]() { //car power
                    sdb::PsdNotPsd event = SIGNAL.getCsdPowBut();
                    VHALOG.d("HW_CUSTOM_INPUT event [power] : %d",event);
                    setScreenKeyValue(KEYCODE_CAR_POWER,event);
                },SignalRX::CsdPowBut)
            .subscribeSignals(
                [=]() { //race mode
                    sdb::PsdNotPsd event = SIGNAL.getSteerWhlLeBtn();
                    VHALOG.d("HW_CUSTOM_INPUT event [race] : %d",event);
                    setScreenKeyValue(KEYCODE_CAR_RACE_MODE,event);
                },SignalRX::SteerWhlLeBtn);
}